package colony.main;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

import colony.Agrupamento;
import colony.Colony;
import colony.DistanceMatrix;
import colony.Engine;
import colony.Grid;
import colony.Item;

public class Main {

	private static Item parseLine(String line) {

		String[] tokens = line.split(",");
		Item.dimension = tokens.length - 1;

		double norma = 0;
		// System.out.println("***PARSE");
		List<Double> values = new ArrayList<Double>();
		for (int i = 0; i < tokens.length - 1; ++i) {
			double v = Double.parseDouble(tokens[i]);
			// System.out.println(v);
			norma += v * v;
			values.add(v);
		}

		norma = Math.sqrt(norma);
		List<Double> newValues = new ArrayList<Double>();
		for (Double v : values) {
			double a = v / norma;
			// System.out.println(a);
			newValues.add(a);
		}
		values = newValues;

		/*
		 * norma = 0; for(Double v : values){ norma += v*v; }
		 * System.out.println(Math.sqrt(norma));
		 */

		return new Item(values, tokens[tokens.length - 1]);

	}

	private static List<Item> readInput(String arquivo) {
		Scanner input = null;
		List<Item> itens = new ArrayList<Item>();

		int id = 0;
		try {
			input = new Scanner(new File(arquivo));
			while (input.hasNextLine()) {
				String line = input.nextLine();
				Item item = parseLine(line);
				item.setId(id);
				itens.add(item);
				id++;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			input.close();
		}
		return itens;
	}

	public static void main(String args[]) {

		Scanner in = new Scanner(System.in);

		String file = args[0];
		double numAntsF = Double.parseDouble(args[1]);
		int gridSizeF = Integer.parseInt(args[2]);
		int neigh = Integer.parseInt(args[3]);
		int run = Integer.parseInt(args[4]);

		List<Item> items = readInput(file);

		Engine.numAnts = (int) Math.round(numAntsF * items.size());
		Engine.gridSize = (int) Math.round(Math.sqrt(gridSizeF * items.size()));
		Engine.neighborhoodSize = neigh;
		Engine.runs = 100;// run * items.size();

		Engine.seed = Math.round(Engine.numAnts * Engine.gridSize
				* Engine.neighborhoodSize * Engine.runs * Math.random());
		Engine.rand = new Random(Engine.seed);
		// 2000 * items.size() > iteracoes ? 2000 * items.size() : iteracoes;

		DistanceMatrix distance = new DistanceMatrix(items);
		// distance.print();
		// in.nextByte();

		Grid grid = new Grid(items, distance);
		// System.out.println("\n\nSTART:");
		// grid.print();

		// in.nextInt();

		Colony colony = new Colony(items, grid);
		colony.preAlgorithm();
		for (int i = 0; i < Engine.runs; i++) {
			System.out.println("iteracao: " + i);
			colony.algorithm();
		}
		System.out.println("Finishing...");
		colony.finish();
		System.out.println("Finished!");

		// System.out.println("\n\nFINISH:");

//		int matrix[][] = new int[Engine.gridSize][Engine.gridSize];
//		for (Item i : items) {
//			matrix[i.getPosition().x][i.getPosition().y] = i.getClasse();
			// System.out.println("id: " + i.getId() + " x: " +
			// i.getPosition().x
			// + " y: " + i.getPosition().y);
//		}

		// for (int i = 0; i < matrix.length; i++) {
		// for (int j = 0; j < matrix[i].length; j++) {
		// System.out.print(matrix[i][j] + " ");
		// }
		// System.out.println();
		// }

		// Agrupamento.print();
		Agrupamento.process(items);
	}

}
